<?php namespace App\Admin\Upload;

use Qiniu\Auth;
use Qiniu\Storage\BucketManager;

trait QiniuService {
    
    //基础服务
    use \App\Core\Service\Service;
    
    protected $bucket ; 
    protected $accessKey ;
    protected $secretKey ;
    protected $callbackUrl ;
    protected $callbackBody ;
    protected $auth;  

    protected function bootService(){
        $this->bootBaseService();
        $this->bucket = env('QINIU_BUCKET'); //当前操作的bucket
		$this->accessKey = env('QINIU_ACCESS_KEY');
		$this->secretKey = env('QINIU_SECRET_KYE');
        $this->callbackUrl = env('QINIU_CALLBACK_UTL');
        $this->callbackBody = env('QINIU_CALLBACK_BODY');
        $this->auth = new Auth($this->accessKey, $this->secretKey);
    }

    //获取上传令牌
	public function getToken(){
		$policy = [
			'callbackUrl' => $this->callbackUrl,
			'callbackBody' => $this->callbackBody,
		];
        return $this->success($this->auth->uploadToken($this->bucket, null, 3600, $policy));
	}
    
    /**
     * 获取指定账号下所有的空间名。
     *
     * @return string[] 包含所有空间名
     */
    public function buckets()
    {
        $bucketMgr = new BucketManager($this->auth);
        return $bucketMgr->buckets();
    }
    
    /**
     * 列取空间的文件列表
     *
     * @param $prefix     指定前缀，只有资源名匹配该前缀的资源会被列出。
     * @param $marker     上一次列举返回的位置标记，作为本次列举的起点信息。
     * @param $limit      单次列举个数限制
     *
     * @return array    包含文件信息的数组，类似：[
     *                                              {
     *                                                 "hash" => "<Hash string>",
     *                                                  "key" => "<Key string>",
     *                                                  "fsize" => "<file size>",
     *                                                  "putTime" => "<file modify time>"
     *                                              },
     *                                              ...
     *                                            ]
     */
    public function listFiles($prefix=null,$marker=null,$limit=1000){
        $bucketMgr = new BucketManager($this->auth);
        list($iterms, $marker, $err) = $bucketMgr->listFiles($this->bucket, $prefix, $marker, $limit);
        if ($err !== null) {
            echo "\n====> list file err: \n";
            var_dump($err);
        } else {
            echo "Marker: $marker\n";
            echo "\nList Iterms====>\n";
            var_dump($iterms);
        }
    }
    
    /**
     * 获取资源的元信息，但不返回文件内容
     *
     * @param $bucket     待获取信息资源所在的空间
     * @param $key        待获取资源的文件名
     *
     * @return array    包含文件信息的数组，类似：
     *                                              [
     *                                                  "hash" => "<Hash string>",
     *                                                  "key" => "<Key string>",
     *                                                  "fsize" => "<file size>",
     *                                                  "putTime" => "<file modify time>"
     *                                              ]
     *
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/stat.html
     */
    public function stat($key)
    {
        $bucketMgr = new BucketManager($this->auth);
        return $bucketMgr->stat($this->bucket, $key);
    }

    /**
     * 删除指定资源
     *
     * @param $bucket     待删除资源所在的空间
     * @param $key        待删除资源的文件名
     *
     * @return mixed      成功返回NULL，失败返回对象Qiniu\Http\Error
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/delete.html
     */
    public function delete($bucket, $key)
    {
        $bucketMgr = new BucketManager($this->auth);
        return $bucketMgr->delete($this->bucket, $key);
    }

    /**
     * 给资源进行重命名，本质为move操作。
     *
     * @param $bucket     待操作资源所在空间
     * @param $oldname    待操作资源文件名
     * @param $newname    目标资源文件名
     *
     * @return mixed      成功返回NULL，失败返回对象Qiniu\Http\Error
     */
    public function rename( $oldname, $newname)
    {
        $bucketMgr = new BucketManager($this->auth);
        return $bucketMgr->rename($this->bucket, $oldname, $newname);
    }

    /**
     * 给资源进行重命名，本质为move操作。
     *
     * @param $from_bucket     待操作资源所在空间
     * @param $from_key        待操作资源文件名
     * @param $to_bucket       目标资源空间名
     * @param $to_key          目标资源文件名
     *
     * @return mixed      成功返回NULL，失败返回对象Qiniu\Http\Error
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/copy.html
     */
    public function copy( $from_key, $to_bucket, $to_key)
    {
        $bucketMgr = new BucketManager($this->auth);
        $from_bucket = $this->bucket;
        return $bucketMgr->copy($from_bucket, $from_key, $to_bucket, $to_key);
    }

    /**
     * 将资源从一个空间到另一个空间
     *
     * @param $from_bucket     待操作资源所在空间
     * @param $from_key        待操作资源文件名
     * @param $to_bucket       目标资源空间名
     * @param $to_key          目标资源文件名
     *
     * @return mixed      成功返回NULL，失败返回对象Qiniu\Http\Error
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/move.html
     */
    public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
    {
        $bucketMgr = new BucketManager($this->auth);
        $from_bucket = $this->bucket;
        return $bucketMgr->move($from_bucket, $from_key, $to_bucket, $to_key);
    }
    
    
    
    
}